package sdk

/*
   Copyright 2016 Alexander I.Grafov <grafov@gmail.com>
   Copyright 2016-2019 The Grafana SDK authors

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

   ॐ तारे तुत्तारे तुरे स्व
*/

import (
	
	
	
	
	
)

type BoolString struct {
	Flag  bool
	Value string
}

func ( *BoolString) ( []byte) error {
	if  == nil || bytes.Equal(, []byte(`"null"`)) {
		return nil
	}
	var (
		 string
		 error
	)
	if [0] != '"' {
		if bytes.Equal(, []byte("true")) {
			.Flag = true
			return nil
		}
		if bytes.Equal(, []byte("false")) {
			return nil
		}
		return errors.New("bad boolean value provided")
	}
	if  = json.Unmarshal(, &);  != nil {
		return 
	}
	.Value = 
	return nil
}

func ( BoolString) () ([]byte, error) {
	if .Value != "" {
		var  bytes.Buffer
		.WriteRune('"')
		.WriteString(.Value)
		.WriteRune('"')
		return .Bytes(), nil
	}
	return strconv.AppendBool([]byte{}, .Flag), nil
}

type BoolInt struct {
	Flag  bool
	Value *int64
}

func ( *BoolInt) ( []byte) error {
	if  == nil || bytes.Equal(, []byte(`"null"`)) {
		return nil
	}
	var (
		 int64
		 error
	)
	if ,  = strconv.ParseInt(string(), 10, 64);  != nil {
		if bytes.Equal(, []byte("true")) {
			.Flag = true
			return nil
		}
		if bytes.Equal(, []byte("false")) {
			return nil
		}
		return errors.New("bad value provided")
	}
	.Value = &
	return nil
}

func ( BoolInt) () ([]byte, error) {
	if .Value != nil {
		return strconv.AppendInt([]byte{}, *.Value, 10), nil
	}
	return strconv.AppendBool([]byte{}, .Flag), nil
}

func ( int64) *IntString {
	return &IntString{
		Value: ,
		Valid: true,
	}
}

// IntString represents special type for json values that could be strings or ints: 100 or "100"
type IntString struct {
	Value int64
	Valid bool
}

// UnmarshalJSON implements custom unmarshalling for IntString type
func ( *IntString) ( []byte) error {
	if  == nil || bytes.Equal(, []byte(`"null"`)) || bytes.Equal(, []byte(`""`)) {
		return nil
	}

	 := string()
	if rune([0]) == '"' {
		 = strings.Trim(, `"`)
	}

	,  := strconv.ParseInt(, 10, 64)
	if  != nil {
		return 
	}

	.Value = 
	.Valid = true

	return nil
}

// MarshalJSON implements custom marshalling for IntString type
func ( *IntString) () ([]byte, error) {
	if .Valid {
		 := strconv.FormatInt(.Value, 10)
		return []byte(), nil
	}

	return []byte(`"null"`), nil
}

func ( float64) *FloatString {
	return &FloatString{
		Value: ,
		Valid: true,
	}
}

// FloatString represents special type for json values that could be strings or ints: 100 or "100"
type FloatString struct {
	Value float64
	Valid bool
}

// UnmarshalJSON implements custom unmarshalling for FloatString type
func ( *FloatString) ( []byte) error {
	if  == nil || bytes.Equal(, []byte(`"null"`)) || bytes.Equal(, []byte(`""`)) {
		return nil
	}

	 := string()
	if rune([0]) == '"' {
		 = strings.Trim(, `"`)
	}

	,  := strconv.ParseFloat(, 64)
	if  != nil {
		return 
	}

	.Value = 
	.Valid = true

	return nil
}

// MarshalJSON implements custom marshalling for FloatString type
func ( *FloatString) () ([]byte, error) {
	if .Valid {
		 := strconv.FormatFloat(.Value, 'g', -1, 64)
		return []byte(), nil
	}

	return []byte(`"null"`), nil
}

// StringSliceString represents special type for json values that could be
// strings or slice of strings: "something" or ["something"].
type StringSliceString struct {
	Value []string
	Valid bool
}

// UnmarshalJSON implements custom unmarshalling for StringSliceString type.
func ( *StringSliceString) ( []byte) error {
	if  == nil || bytes.Equal(, []byte(`"null"`)) {
		return nil
	}

	// First try with string.
	var  string
	if  := json.Unmarshal(, &);  == nil {
		.Value = []string{}
		.Valid = true
		return nil
	}

	// Lastly try with string slice.
	var  []string
	 := json.Unmarshal(, &)
	if  != nil {
		return 
	}

	.Value = 
	.Valid = true
	return nil
}

// MarshalJSON implements custom marshalling for StringSliceString type.
func ( *StringSliceString) () ([]byte, error) {
	if !.Valid {
		return []byte(`"null"`), nil
	}

	return json.Marshal(.Value)
}